<template>
  <div>
    <el-dialog :title="title"
      :visible.sync="dialogVisible"
      width="50%"
      top="5vh"
      @close.native="cancel"
      :close-on-click-modal="false">
      <el-form ref="ruleForm"
        :model="form"
        :rules="rules"
        label-width="160px">
        <el-form-item label="排序"
          prop="order">
          <el-input v-model="form.order"
            placeholder="请输入"
            :disabled="isLook=='look'" />
        </el-form-item>
        <el-form-item prop="unit">
          <template slot="label">
            <span>大单位名称</span>
            <el-tooltip class="item"
              effect="dark"
              content="战区（东部、南部、西部、北部、中部）、军兵种"
              placement="top">
              <i class="el-icon-question"
                style="color:#2c68ff" />
            </el-tooltip>
            <span>:</span>
          </template>
          <el-input v-model="form.unit"
            placeholder="请输入"
            :disabled="isLook=='look'" />
        </el-form-item>
        <el-form-item prop="department">
          <template slot="label">
            <span>职能部门名称</span>
            <el-tooltip class="item"
              effect="dark"
              content="大单位负责科研设施与仪器共享管理的内设机构名称，如XX部XX局XX处"
              placement="top">
              <i class="el-icon-question"
                style="color:#2c68ff" />
            </el-tooltip>
            <span>:</span>
          </template>
          <el-input v-model="form.department"
            placeholder="请输入"
            type="textarea"
            rows="3"
            :disabled="isLook=='look'" />
        </el-form-item>
        <el-form-item prop="name">
          <template slot="label">
            <span>联系人姓名</span>
            <el-tooltip class="item"
              effect="dark"
              content="大单位职能部门具体负责科研设施与仪器共享管理的人员"
              placement="top">
              <i class="el-icon-question"
                style="color:#2c68ff" />
            </el-tooltip>
            <span>:</span>
          </template>
          <el-input v-model="form.name"
            placeholder="请输入"
            :disabled="isLook=='look'" />
        </el-form-item>
        <el-form-item label="办公电话："
          prop="workPhone">
          <el-input v-model="form.workPhone"
            placeholder="请输入"
            type="textarea"
            :rows="4"
            :disabled="isLook=='look'" />
        </el-form-item>
        <el-form-item label="移动电话："
          prop="phone">
          <el-input v-model="form.phone"
            placeholder="请输入"
            :disabled="isLook=='look'" />
        </el-form-item>
      </el-form>
      <div slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          @click="submitForm"
          v-if="isLook!=='look'">确 定</el-button>
        <el-button @click="
          cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { uploadFile } from '@/api/common.js'
import {
  insertArticle,
  editArticle
  // articleDetail
} from '@/api/publishManage.js'
export default {
  data() {
    return {
      dialogVisible: false,
      isLook: '',
      accept: '*',
      title: '',
      form: {
        title: '',
        department: '',
        name: '',
        workPhone: '',
        phone: '',
        fileId: null,
        fileName: null
      },
      facilityType: ['政策法规', '管理制度'],
      file: null,
      rules: {
        title: [
          { required: true, message: '大单位名称不能为空！', trigger: 'blur' }
        ],
        department: [
          { required: true, message: '职能部门不能为空！', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '联系人不能为空！', trigger: 'blur' }
        ],
        workPhone: [
          { required: true, message: '办公电话不能为空', trigger: 'blur' }
        ],
        phone: [{ required: true, message: '移动电话为空', trigger: 'blur' }]
      }
    }
  },
  created() {},
  methods: {
    show(obj) {
      this.dialogVisible = true
      this.title = obj.title
      this.isLook = obj.isLook
      if (obj.data) {
        this.form = Object.assign(obj.data)
        // articleDetail({ articleId: obj.data.articleId }).then(res => {
        //   this.form = res.data
        // })
      }
    },
    submitForm() {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          if (this.file) {
            const formData = new FormData()
            formData.append('file', this.file)
            formData.append('pathType', this.file.name.split('.').pop())
            uploadFile(formData).then(res => {
              this.form.fileId = res.data.fileId
              if (this.form.articleId) {
                editArticle(this.form).then(() => {
                  this.cancel()
                })
              } else {
                insertArticle(this.form).then(() => {
                  this.cancel()
                })
              }
            })
          } else {
            if (this.form.articleId) {
              editArticle(this.form).then(() => {
                this.cancel()
              })
            } else {
              insertArticle(this.form).then(() => {
                this.cancel()
              })
            }
          }
        } else {
          return false
        }
      })
    },
    cancel() {
      this.form = this.$options.data().form
      this.$refs.ruleForm.clearValidate()
      this.dialogVisible = false
      this.isLook = false
      this.file = null
      this.$emit('close')
    },
    handleChange(file) {
      this.file = file.raw
      this.form.fileName = file.name
    },
    // 多次上传覆盖
    handleExceed(file) {
      console.log('[ file ] >', file)
      this.$refs.uploadRef.clearFiles()
      // this.handleChange(file[0])
      this.file = file[0]
      this.form.fileName = file[0].name
    },
    downloadLink(item) {
      this.downloadByUrl(item.fileName, item.filePath)
    },
    closeFile() {
      this.file = null
      this.$refs.uploadRef.clearFiles()
    }
  }
}
</script>

<style lang="less" scoped>
:deep(.el-table .cell) {
  font-size: 16px;
}
:deep(.el-table th > .cell) {
  font-size: 18px;
}
</style>
